package com.hmall.item.web;


import com.hmall.common.dto.PageDTO;
import com.hmall.common.dto.SearchDTO;
import com.hmall.common.pojo.Item;
import com.hmall.common.vo.SearchVo;
import com.hmall.item.service.IItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/list")
    public PageDTO<Item> page(@RequestParam Integer page, @RequestParam Integer size){

        return itemService.getPage(page,size);
    }

    /**
     * 根据id查询商品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Item getById(@PathVariable("id") Long id){
        return itemService.getById(id);
    }


    /**
     * 修改商品
     * @param item
     */
    @PutMapping
    public void updateGood(@RequestBody Item item){
        itemService.updateGood(item);
    }

    /**
     * 新增商品
     * @param item
     */
    @PostMapping
    public void save(@RequestBody Item item){
        item.setCreateTime(new Date());
        item.setUpdateTime(new Date());
        itemService.save(item);
    }

    /**
     * 上架下架
     * @param id
     * @param status
     */
    @PutMapping("/status/{id}/{status}")
    public void updateStatus(@PathVariable("id") Long id,@PathVariable("status") Integer status){
        itemService.updateStatus(id,status);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") Long id){
        itemService.removeById(id);
    }

    @PostMapping("/search/filters")
    public SearchVo searchFilters(){
        return itemService.searchFilters();
    }

    @PostMapping("/search/list")
    public PageDTO<Item> searchList(@RequestBody SearchDTO dto){
        return itemService.searchList(dto);
    }

    /**
     * 扣减库存
     * @param itemId
     * @param num
     */
    @PutMapping("/stock/{itemId}/{num}")
    public void updateStock(@PathVariable("itemId") Long itemId,@PathVariable("num") Integer num){
        itemService.updateStock(itemId,num);
    }
    @PostMapping("/restock/{itemId}/{num}")
    public void restock(@PathVariable("itemId") Long itemId,@PathVariable("num") Integer num){
        itemService.updateRestock(itemId,num);
    }

}
